/**
 * 笔试强训day2
 */
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.*;
public class Test {
    //1.牛牛的快递（BC64）
    //https://www.nowcoder.com/practice/41b42e7b3c3547e3acf8e90c41d98270?tpId=290&tqId=39852&ru=/exam/oj
    public static void main1(String[] args) {
        //1.输入一个整数 和 字符
        Scanner in = new Scanner(System.in);
        double k = in.nextDouble();
        char c = in.next().charAt(0);

        //判断kg
        int cost = 0;
        if(k <= 1.0){
           cost  =20;
        }else{
            cost = 20 + (int)Math.ceil(k - 1);
        }

        //判断是否加急
        if(c == 'y'){
            cost += 5;
        }

        System.out.println(cost);
    }

    //2.DP4最小花费楼梯
    //https://www.nowcoder.com/practice/9b969a3ec20149e3b870b256ad40844e?tpId=230&tqId=39751&ru=/exam/oj
    public static void main2(String[] args) {
        //1.输入
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();

        int[] cost = new int[n];
        int[] dp = new int[n + 1];

        for(int i =0; i < n;i ++){
            cost[i] = in.nextInt();
        }

        //2.初始化
        dp[0] = 0;
        dp[1] = 0;
        for(int j = 2;j <= n;j++){
            dp[j] = Math.min((dp[j -1] + cost[j - 1]),(dp[j -2] + cost[j -2]));
        }
        if(n == 1){
            System.out.println(cost[0]);
        }else{
            System.out.println(dp[n]);
        }
    }

    //3.数组中两个字符串的最小距离
    //https://www.nowcoder.com/questionTerminal/2c6a0a8e1d20492f92941400036e0890
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String[] string = br.readLine().split(" ");
        String str1 = string[0],str2 = string[1];

        //预处理
        int prev1 = -1,prev2 = -1,ret = 0xff4011;

        for(int i = 0; i < n;i++){
            String str = br.readLine();
            //判断
            if(str.equals(str1)){
                if(prev2 != -1)
                    ret = Math.min(i - prev2,ret);
                prev1 = i;
            }else if(str.equals(str2)){
                if(prev1 != -1)
                       ret = Math.min(i - prev1,ret);
                prev2 = i;
            }
        }

        if(ret == 0xff4011){
            System.out.println(-1);
        }else{
            System.out.println(ret);
        }
    }
}
